<?php
namespace app\admin\controller;

use think\Controller;
use Common\Controller\BaseController;
use think\Request;
use think\Session;

/*
 * 2019-07-17-16:55
 * name:DMH
 *
 * */
class Meifeng extends Controller
{
    //首页
    public function index(){
        $res = db('user_meifeng')->order('id Desc')->paginate(10);
        $page =  $res->render();
        $this->assign('page',$page);
        $this->assign('list',$res);
        return $this->fetch();
    }
//产品

    public function chanpin(){
        $res = db('chanpin')->order('id Desc')->paginate(10);
        $page =  $res->render();
        $this->assign('page',$page);
        $this->assign('list',$res);
        return $this->fetch();
    }
    public function chanpininsert(){
        return $this->fetch();
    }
    public function chanpin1(){
        //var_dump($_POST);exit;
        //修改方案

        if($_POST['type'] == 'meifengupdata'){
            $id['id'] = $_POST['id'];
            $userimg = request()->file("img");
            if($userimg) {
                $cc = $userimg->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
                $img = $cc->getSaveName();
                $data['img'] = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/'.$img;
            }

            $data['title'] = trim($_POST['title']);
            //$data['details'] = $_POST['details'];
            $data['addtime'] = time();
            //var_dump($data);exit;
            $res = db('chanpin')->where($id)->update($data);
            if($res){
                echo "<script>alert('操作成功');javascript:history.go(-1);</script>";
            }else{
                echo "<script>alert('操作失败');javascript:history.go(-1);</script>";
            }
        }else {
            $userimg = request()->file("img");
            if($userimg) {
                $cc = $userimg->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
                $img = $cc->getSaveName();
                $data['img'] = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/'.$img;
            }

            $data['title'] = trim($_POST['title']);
            //$data['details'] = $_POST['details'];
            $data['addtime'] = time();
            $res = db('chanpin')->insert($data);
            if ($res) {
                echo "<script>alert('操作成功');javascript:history.go(-1);</script>";
            } else {
                echo "<script>alert('操作失败');javascript:history.go(-1);</script>";
            }
        }
    }
    //产品修改

    public function chanpinupdata(){
        $id['id'] = $_GET['id'];
        $res = db('chanpin')->where($id)->find();
        $this->assign('list',$res);
        return $this->fetch();
    }
    //添加与修改
    public function meifengupdata(){
        $id['id'] = $_GET['id'];
        $res = db('user_meifeng')->where($id)->find();
        $this->assign('list',$res);
        return $this->fetch();
    }
    //添加
    public function meifenginsert(){
        return $this->fetch();
    }
    public function meifeng(){
        //var_dump($_POST);exit;
    //修改方案

        if($_POST['type'] == 'meifengupdata'){
        $id['id'] = $_POST['id'];
            $userimg = request()->file("img");
            if($userimg) {
                $cc = $userimg->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
                $img = $cc->getSaveName();
                $data['img'] = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/'.$img;
            }

            $data['title'] = trim($_POST['title']);
            $data['details'] = $_POST['details'];
            $data['addtime'] = time();
            $res = db('user_meifeng')->where($id)->update($data);
            if($res){
                echo "<script>alert('操作成功');javascript:history.go(-1);</script>";
            }else{
                echo "<script>alert('操作失败');javascript:history.go(-1);</script>";
            }
        }else {
            $userimg = request()->file("img");
            if($userimg) {
                $cc = $userimg->move(ROOT_PATH . 'public' . DS . 'uploads');//图片保存路径
                $img = $cc->getSaveName();
                $data['img'] = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/'.$img;
            }

            $data['title'] = trim($_POST['title']);
            $data['details'] = $_POST['details'];
            $data['addtime'] = time();
            $res = db('user_meifeng')->insert($data);
            if ($res) {
                echo "<script>alert('操作成功');javascript:history.go(-1);</script>";
            } else {
                echo "<script>alert('操作失败');javascript:history.go(-1);</script>";
            }
        }
    }
    //删除
    public function meifengdel(){
        if(!$_GET){
            $this->success('提交不能为空','express');
        }
        $data['id'] = trim($_GET['id']);
        $res=db('user_meifeng')->where($data)->delete();
        if($res){
            echo "<script>alert('删除信息成功');javascript:history.go(-1);</script>";
            //return ['code'=>1,'msg'=>'删除信息成功'];

        }else{
            echo "<script>alert('删除信息失败');javascript:history.go(-1);</script>";
            //return ['code'=>0,'msg'=>'删除信息失败'];
        }
    }
    public function chanpindel(){
        if(!$_GET){
            $this->success('提交不能为空','express');
        }
        $data['id'] = trim($_GET['id']);
        $res=db('chanpin')->where($data)->delete();
        if($res){
            echo "<script>alert('删除信息成功');javascript:history.go(-1);</script>";
            //return ['code'=>1,'msg'=>'删除信息成功'];

        }else{
            echo "<script>alert('删除信息失败');javascript:history.go(-1);</script>";
            //return ['code'=>0,'msg'=>'删除信息失败'];
        }
    }
    //美缝咨询
    public function zixun(){
        $res = db('user_zixun')->order('id Desc')->paginate(10);
        $page =  $res->render();
        $this->assign('page',$page);
        $this->assign('list',$res);
        return $this->fetch();
    }
    //换图片
    public function tihuan(){
        return $this->fetch();
    }
    public function tihuanupdata(){

        if(request()->file("img1")){
            $userimg1 = request()->file("img1");
            $name = '1.png';
            $cc = $userimg1->move(ROOT_PATH . 'public' . DS . 'background',$name);//图片保存路径
            $img = $cc->getSaveName();
            }
        if(request()->file("img2")){
            $userimg2 = request()->file("img2");
            $name = '2.png';
            $cc = $userimg2->move(ROOT_PATH . 'public' . DS . 'background',$name);//图片保存路径
            $img = $cc->getSaveName();
        }
        if(request()->file("img3")){
            $userimg3 = request()->file("img3");
            $name = '3.png';
            $cc = $userimg3->move(ROOT_PATH . 'public' . DS . 'background',$name);//图片保存路径
            $img = $cc->getSaveName();
        }
        if(request()->file("img4")){
            $userimg4 = request()->file("img4");
            $name = '4.png';
            $cc = $userimg4->move(ROOT_PATH . 'public' . DS . 'background',$name);//图片保存路径
            $img = $cc->getSaveName();
        }

        if($img){
            echo "<script>alert('更新成功');javascript:history.go(-1);</script>";
        }else{
            echo "<script>alert('更新失败');javascript:history.go(-1);</script>";
        }

    }
}